package com.talent.controller;

import com.talent.entities.dto.Admin;
import com.talent.entities.dto.User;
import com.talent.entities.result.CommonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author 周意朗
 * Date: 2022/3/21
 * @since JDK 11
 * ipaddress:82
 */
@RestController
@Slf4j
@CrossOrigin
public class LoginController {
    @Resource
    private RestTemplate restTemplate;

    @Value("${service-url.nacos-user-service}")
    private String serverURL;

    /**
     * 测试用链接,使用来测试是否可以ping通服务
     * http://localhost:82/consumer/logintest/13
     *
     * @param id       随便写的一个id
     * @param request  前端传递的,用来解决同局域网连接问题
     * @param model    前端传递的,用来解决同局域网连接问题
     * @param response 前端传递的,用来解决同局域网连接问题
     * @return 返回是否可以ping通的字符串
     */
    @GetMapping(value = "/consumer/logintest/{id}")
    public String getReady(@PathVariable("id") Long id,
                           HttpServletRequest request,
                           Model model,
                           HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Access-Control-Allow-Headers", "*");
        response.setHeader("Access-Control-Allow-Methods", "*");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Max-Age", "3600");
        String url = serverURL + "/logintest/" + id;
        return restTemplate.getForObject(url, String.class);
    }

    /**
     * 为用户创建的login方法
     *
     * @param user     前端传递进来的user,包含手机号和密码
     * @param request  前端传递的,用来解决同局域网连接问题
     * @param model    前端传递的,用来解决同局域网连接问题
     * @param response 前端传递的,用来解决同局域网连接问题
     * @return 封装类, 包含成功与否的代码与用户信息
     */
    @PostMapping("/login")
    @ResponseBody
    public CommonResult<User> loginForUser(@RequestBody User user,
                                           HttpServletRequest request,
                                           Model model,
                                           HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Headers", "*");
        response.setHeader("Access-Control-Allow-Methods", "*");
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Max-Age", "3600");
        User getUser = restTemplate.getForObject(serverURL + "/provider/loginforuser/" + user.getUserTel() + "/" + user.getUserPwd(), User.class);
        if (getUser == null) {
            return new CommonResult<>(400, "该手机号尚未注册,请先注册");
        }
        if ("0000".equals(getUser.getUserTel())) {
            return new CommonResult<>(401, "请检查用户名或密码是否正确");
        }
        return new CommonResult<>(200, "success", getUser);
    }

    @PostMapping("/loginforadmin")
    @ResponseBody
    public CommonResult<Admin> loginForAdmin(@RequestBody Admin admin,
                                             HttpServletRequest request,
                                             Model model,
                                             HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Access-Control-Allow-Headers", "*");
        response.setHeader("Access-Control-Allow-Methods", "*");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Max-Age", "3600");
        Admin getAdmin = restTemplate.getForObject(serverURL + "/provider/loginofadmin/" + admin.getAdminPhone() + "/" + admin.getAdminPwd(), Admin.class);
        if (getAdmin == null) {
            return new CommonResult<>(400, "用户名或密码错误");
        }
        if ("0000".equals(getAdmin.getAdminPhone())) {
            return new CommonResult<>(401, "请检查用户名或密码是否正确");
        }
        return new CommonResult<>(200, "success", getAdmin);
    }

    @PostMapping("/userupdate/pwd")
    @ResponseBody
    public CommonResult<String> updateUserPwd(@RequestBody User user,
                                              HttpServletRequest request,
                                              Model model,
                                              HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Access-Control-Allow-Headers", "*");
        response.setHeader("Access-Control-Allow-Methods", "*");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Max-Age", "3600");
        Boolean flag = restTemplate.postForObject(serverURL + "/provider/userpwdchange", user, Boolean.class);
        if (flag == null) {
            return new CommonResult<>(401, "fail", "更新失败,请检查身份信息");
        }
        if (Boolean.TRUE.equals(flag)) {
            return new CommonResult<>(200, "success", "更新成功");
        }
        return new CommonResult<>(400, "fail", "更新失败,请检查输入");
    }

    @PostMapping("/userupdate/info")
    @ResponseBody
    public CommonResult<User> updateUserInfo(@RequestBody User user,
                                             HttpServletRequest request,
                                             Model model,
                                             HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Headers", "*");
        response.setHeader("Access-Control-Allow-Methods", "*");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Access-Control-Max-Age", "3600");
        User user1 = restTemplate.getForObject(serverURL + "/provider/userudt/" + user.getUserTel() + "/" + user.getUserEmail() + "/" + String.valueOf(user.getUserAreaId()), User.class);
        if (user1 == null) {
            return new CommonResult<>(400, "please check info", null);
        }
        return new CommonResult<>(200, "success", user1);
    }

    @PostMapping("/registuser")
    @ResponseBody
    public CommonResult<String> registerUser(
            @RequestBody User register,
            HttpServletRequest request,
            Model model,
            HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Headers", "*");
        response.setHeader("Access-Control-Allow-Methods", "*");
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Access-Control-Max-Age", "3600");
        Boolean flag = restTemplate.postForObject(serverURL + "/provider/registuser", register, Boolean.class);
        System.out.println(register.toString());
        if (Boolean.TRUE.equals(flag)) {
            return new CommonResult<>(200, "success", "注册成功");
        } else {
            return new CommonResult<>(400, "fail", "请检查该身份证或者手机是否已经注册");
        }
    }

}

